/**
 * react hook 实现倒计时
 */
import { useState, useEffect } from 'react'; //导入useState和useEffect

export const useCountdown = (initialCount) => {
  const [count, setCount] = useState(initialCount);

  useEffect(() => {
    const interval = setInterval(() => {
      setCount(count - 1);
    }, 1000);

    return () => clearInterval(interval);
  }, [count]);
};
/**
 * vue3 hook 实现倒计时
 * 
 */
import { ref, onMounted, onUnmounted } from 'vue';

export const useCountdown_vue = (initialCount) => {
  const count = ref(initialCount);
  let intervalId = null; // 在外部作用域声明，确保可以被清理

  const startCountdown = () => {
    intervalId = setInterval(() => {
      count.value--;
      if (count.value <= 0) {
        clearInterval(intervalId);
        intervalId = null;
      }
    }, 1000);
  };

  onMounted(() => {
    startCountdown();
  });

  onUnmounted(() => {
    clearInterval(intervalId); // 组件卸载时清理定时器
  });

  return {
    count,
    startCountdown
  };
};
